#include "iBe.h"

GLMeshPtr MeshLoader::loadFromDatabase2(Database &_db, const String &_name)
{
  GLMeshPtr retGLMesh;

  SqlResult2<SQLITE_FLOAT, 6>::Array result1;
  SqlResult2<SQLITE_INTEGER, 1>::Array result2;

  /*
    SqlResult<SQLITE_FLOAT, SQLITE_FLOAT, SQLITE_FLOAT, 
    SQLITE_FLOAT, SQLITE_FLOAT, SQLITE_FLOAT, SQLITE_NULL>::Array result1;
    SqlResult<SQLITE_INTEGER, SQLITE_NULL, SQLITE_NULL, 
    SQLITE_NULL, SQLITE_NULL, SQLITE_NULL, SQLITE_NULL>::Array result2;
  */

  String sql1 = "", sql2 = "";

  Turple3fArray vertexArray;
  Turple1iArray indexArray;
  Turple3f vertex;
  Turple1i index;

  sql1 += "SELECT x,y,z,r,g,b FROM vertex WHERE name = '";
  sql1 += _name;
  sql1 += "' order by vertex_id;";

  sql2 += "SELECT vertex_id FROM face WHERE name = '";
  sql2 += _name;
  sql2 += "' order by ord;";

  if (!_db.query2(sql1, result1))
    {
      std::cout << "Sql1 error: " << sql1 << std::endl;
      return retGLMesh;
    }

  for(unsigned int i = 0; i < result1.size(); ++i)
    {
      vertex[0] = result1[i].value[0].value;
      vertex[1] = result1[i].value[1].value;
      vertex[2] = result1[i].value[2].value;
      vertexArray.push_back( vertex );
      
      vertex[0] = result1[i].value[3].value;
      vertex[1] = result1[i].value[4].value;
      vertex[2] = result1[i].value[5].value;

      vertexArray.push_back( vertex );
    }

  //if (!_db.query<SQLITE_INTEGER, SQLITE_NULL, SQLITE_NULL, 
  //SQLITE_NULL, SQLITE_NULL, SQLITE_NULL, SQLITE_NULL>(sql2, result2))
  if (!_db.query2(sql2, result2))
    {
      std::cout << "Sql2 error: " << sql2 << std::endl;
      return retGLMesh;
    }

  for(unsigned int i = 0; i < result2.size(); ++i)
    {
      //index[0] = result2[i].first.value;
      index[0] = result2[i].value[0].value;
      indexArray.push_back( index );
    }

  retGLMesh.reset( new GLMesh() );
  retGLMesh->setName( _name );
  retGLMesh->setIndexedVertices(vertexArray, indexArray );

  return retGLMesh;
}

/*
MeshGLPtr MeshLoader::loadFromDatabase2(Database &_db, const String &_name)
{
  MeshGLPtr retMeshGL;

  SqlResult<SQLITE_INTEGER, SQLITE_NULL, SQLITE_NULL, SQLITE_NULL, SQLITE_NULL, SQLITE_NULL>::Array result1;
  SqlResult<SQLITE_FLOAT, SQLITE_FLOAT, SQLITE_FLOAT, SQLITE_FLOAT, SQLITE_FLOAT, SQLITE_FLOAT>::Array result2;

  String sql1 = "", sql2 = "";

  Mesh2::VertexArray vertexArray;

  sql1 += "SELECT x,y,z,r,g,b FROM vertex WHERE name = '";
  sql1 += _name;
  sql1 += "' order by face.ord;";

  if ( _db.query<SQLITE_INTEGER, SQLITE_NULL, SQLITE_NULL, SQLITE_NULL, SQLITE_NULL, SQLITE_NULL>( sql1 , result1 ) )
    {
      if (_db.query<SQLITE_FLOAT, SQLITE_FLOAT, SQLITE_FLOAT, SQLITE_FLOAT, SQLITE_FLOAT, SQLITE_FLOAT>( sql2, result2 ))
	{
	  Turple<Turple3f, 2> vertex;

	  for(unsigned int i = 0; i < result2.size(); ++i)
	    {
	      vertex[0][0] = result2[i].first.value;
	      vertex[0][1] = result2[i].second.value;
	      vertex[0][2] = result2[i].third.value;

	      vertex[1][0] = result2[i].fourth.value;
	      vertex[1][1] = result2[i].fifth.value;
	      vertex[1][2] = result2[i].sixth.value;

	      vertexArray.push_back( vertex );
	    }

	  retMesh2.reset( new Mesh2() );

	  retMesh2->set(vertexArray);

	}
      else
	{
	  std::cout << "Error:" << sql2 << std::endl;
	}
    }

  return retMesh2;
}
*/

ObjectPtr MeshLoader::loadObjectFromDatabase(Database &_db, const String &_name)
{
  ObjectPtr retObject;

  SqlResult<SQLITE_TEXT, SQLITE_FLOAT, SQLITE_FLOAT, SQLITE_FLOAT, SQLITE_FLOAT, SQLITE_FLOAT, SQLITE_FLOAT>::Array result;

  String sql = "";

  sql += "SELECT mesh_name, x,y,z,a,b,c FROM object WHERE name = '";
  sql += _name;
  sql += "';";

  if (!_db.query<SQLITE_TEXT, SQLITE_FLOAT, SQLITE_FLOAT, SQLITE_FLOAT, SQLITE_FLOAT, SQLITE_FLOAT, SQLITE_FLOAT>( sql, result ))
    {
      return retObject;
    }

  if ( result.size() > 0 )
    {
      float x,y,z;
      float a,b,c;

      x = result[0].second.value;
      y = result[0].third.value;
      z = result[0].fourth.value;

      a = result[0].fifth.value;
      b = result[0].sixth.value;
      c = result[0].seventh.value;

      retObject.reset( new Object() );
      retObject->setName(_name);
      retObject->setMeshName( result[0].first.value );
      retObject->setLocation(x, y, z);
      retObject->setOrientation(a, b); // , c);
   }

  return retObject;
}
